package main

import (
	"cdam/cron"
	_ "cdam/qiniu/qlang/lib/builtin" // 导入 builtin 包
	_ "cdam/qiongqi"
	_ "cdam/routers"
	"cdam/util"
	_ "cdam/util"
	_ "fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/plugins/cors"
	_ "github.com/lib/pq"
	"math/rand"
	"strings"
	"time"
)

var strings_Exports = map[string]interface{}{
	"replacer": strings.NewReplacer,
}

func init() {
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins: true,
		//AllowOrigins:      []string{"https://192.168.0.102"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "UserName", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
	}))
	orm.RegisterDriver("postgres", orm.DRPostgres) //参考：https://beego.me/docs/mvc/model/orm.md#registerdriver
	orm.RegisterDataBase("default", "postgres", "user=postgres password=02311218 dbname=postgres host=localhost port=5432 sslmode=disable")
	orm.RunSyncdb("default", false, true)
	// a := new(auth.Adapter).NewAdapter() // Your driver and data source.
	// e:= casbin.NewEnforcer("conf/rbac_model.conf", a)
	// beego.InsertFilter("*", beego.BeforeRouter,auth.NewAuthorizer(e))
}

func main() {
	orm.Debug = true

	util.Seed = rand.NewSource(time.Now().UnixNano())
	/*type Inventory struct {
		Material string
		Count    uint
	}
	sweaters := Inventory{"wool", 17}
	tmpl, err := template.ParseFiles("template/files/model.go.tpl")

	file, err := os.OpenFile("demo.txt", os.O_CREATE|os.O_WRONLY, 0755)
	err = tmpl.Execute(file, sweaters)

	if err != nil {
		panic(err)
	}*/

	/* beego.InsertFilter("*",beego.BeforeRouter, func(context *context.Context) {
		url := context.Request.RequestURI
		if !strings.Contains(url,"auth/Login") &&
			!strings.Contains(url,"auth/Logout") &&
			!strings.Contains(url,"auth/CheckToken") {
			cookie := context.Request.Header.Get("Authorization")  //["Authorization"]
			user := context.Request.Header.Get("UserName")
			var username string
			if len(cookie) > 0 {
				username, _ = util.CheckToken(cookie)
			}
			if  len(user) <= 0 && username != user {
				context.ResponseWriter.WriteHeader(801)

				byte := []byte("The auth is wrong!!")
				context.ResponseWriter.Write(byte)
				return
			}
		}
	},true)*/
	/*
		qlang.Import("fmt",map[string]interface{}{
			"Print":  fmt.Println})
		ql := qlang.New()
		ql.SetVar("today",6)
		err := ql.Exec([]byte(`fmt.Print(today);if today == 6 { today = "mon" ;} else {fmt.Print("he")} ; nextday = today`),"fi")
		if err == nil {
			result := ql.Context.CopyVars()
			fmt.Print(result["nextday"])
		}
	)*/
	// if beego.BConfig.RunMode == "dev" {


	/* cronExpress := "* * * * * *"
	tk1 := toolbox.NewTask("mytask", cronExpress, SyncFoodTaskSyncFoodTask)
	toolbox.AddTask("mytask", tk1)
	toolbox.StartTask()
	*/

    scheduled := cron.Init()
    scheduled.LoadAllTask()
    scheduled.StartAllTask()
	beego.BConfig.WebConfig.DirectoryIndex = true
	beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	// }
	beego.Run()
}